package com.ibeeking.gmc.global.g.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.entity.TenantVariableDTO;
import com.ibeeking.found.common.entity.TenantVariableParam;
import com.ibeeking.found.common.entity.VariableQuery;
import com.ibeeking.gmc.global.g.rest.service.ITenantVariableService;
import com.ibeeking.gmc.global.service.common.dos.TenantVariableDO;
import com.ibeeking.gmc.global.service.mapper.TenantVariableMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.id.IdGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class TenantVariableServiceImpl extends ServiceImpl<TenantVariableMapper, TenantVariableDO> implements ITenantVariableService {
    @Resource
    private TenantVariableMapper TenantVariableMapper;

    /**
     * 修改
     *
     * @param param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addAndModify(TenantVariableParam param){
        Integer tenantId = param.getTenantId();
        String paramName = param.getPkey();
        if(StringUtils.equalsIgnoreCase(paramName, "h5_domain")) {
            Integer count = this.TenantVariableMapper.selectCount(new LambdaQueryWrapper<TenantVariableDO>()
                    .eq(TenantVariableDO::getPkey, paramName)
                    .eq(TenantVariableDO::getPvalue, param.getPvalue()));
            if (count > 1) return false;
        }
        TenantVariableDO variableDO = this.TenantVariableMapper.selectOne(new LambdaQueryWrapper<TenantVariableDO>()
                .eq(TenantVariableDO::getTenantId, tenantId)
                .eq(TenantVariableDO::getPkey, paramName));
        if(variableDO != null){
            variableDO.setPvalue(param.getPvalue());
            variableDO.setRemark(param.getRemark());
            return this.TenantVariableMapper.updateById(variableDO) > 0;
        }else{
            variableDO = BeanUtil.convertBean(param, TenantVariableDO.class);
            variableDO.setId(IdGenerator.getId());
            return this.TenantVariableMapper.insert(variableDO) >0;
        }
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public TenantVariableDTO queryById(Long id) {
        TenantVariableDO variableDO = this.TenantVariableMapper.selectById(id);
        TenantVariableDTO variableDTO = BeanUtil.convertBean(variableDO, TenantVariableDTO.class);
        return variableDTO;
    }

    /**
     * @param query
     * @return
     */
    @Override
    public TenantVariableDTO queryTenantVariable(VariableQuery query){
        TenantVariableDO variableDO = this.TenantVariableMapper.selectOne(new LambdaQueryWrapper<TenantVariableDO>()
                .eq(query.getTenantId()!=null,TenantVariableDO::getTenantId, query.getTenantId())
                .eq(StringUtils.isNotEmpty(query.getPkey()),TenantVariableDO::getPkey, query.getPkey())
                .eq(StringUtils.isNotEmpty(query.getPvalue()),TenantVariableDO::getPvalue, query.getPvalue()));
        if(variableDO!=null)
            return BeanUtil.convertBean(variableDO, TenantVariableDTO.class);
        return null;
    }
}
